define(["jquery", "jqueryUI", "widget"], function ($, $UI, widget) {
  function Window() {
    this.cfg = {
      content: "",
      title: "系统提示",
      width: 900,
      height: 600,
      hasCloseBtn: true,
      hasMask: true,
      isDraggable: true,
      alertBtnHandler: null,
      closeBtnHandler: null,
      confirmBtnHandler: null,
      cancelBtnHandler: null,
      promptBtnHandler: null,
      winType: "alert"
    };
  }

  Window.prototype = $.extend({}, widget, {
    renderUI: function () {
      var footerContent = "";
      var content = this.cfg.content;
      switch (this.cfg.winType) {
        case "alert":
          footerContent = "<input class='window_alertBtn' type='button' value='确定'>";
          break;
        case "confirm":
          footerContent = "<input class='window_confirmBtn' type='button' value='确定'>" +
              "<input class='window_cancelBtn' type='button' value='取消'>";
          break;
        case "prompt":
          footerContent = "<input class='window_promptBtn' type='button' value='确定'>" +
              "<input class='window_cancelBtn' type='button' value='取消'>";
          content = "<input type='text' class='window_promptInput' value='" + this.cfg.content + "'>";
          break;
      }
      this.boundingBox = $("<div class='window_boundingBox'>" +
          "<div class='window_header'>" + this.cfg.title + "</div>" +
          "<div class='window_body'>" + content + "</div>" +
          "<div class='window_footer'>" + footerContent + "</div>" +
          "</div>");
      if (this.cfg.hasMask) {
        this.mask = $("<div class='window_mask'></div>");
        this.mask.appendTo(document.body);
      }
      if (this.cfg.hasCloseBtn) {
        this.boundingBox.append("<span class='window_closeBtn'>X</span>");
      }
    },
    bindUI: function () {
      var that = this;
      this.boundingBox.delegate(".window_alertBtn", "click", function () {
        that.fire("alert");
        that.destroy();
      }).delegate(".window_closeBtn", "click", function () {
        that.fire("close");
        that.destroy();
      }).delegate(".window_confirmBtn", "click", function () {
        that.fire("confirm");
        that.destroy();
      }).delegate(".window_cancelBtn", "click", function () {
        that.fire("cancel");
        that.destroy();
      }).delegate(".window_promptBtn", "click", function () {
        that.fire("prompt", $(".window_promptInput").val());
        that.destroy();
      });
      if (this.cfg.alertBtnHandler) {
        this.on("alert", this.cfg.alertBtnHandler);
      }
      if (this.cfg.closeBtnHandler) {
        this.on("close", this.cfg.closeBtnHandler);
      }
      if (this.cfg.confirmBtnHandler) {
        this.on("confirm", this.cfg.confirmBtnHandler);
      }
      if (this.cfg.cancelBtnHandler) {
        this.on("cancel", this.cfg.cancelBtnHandler);
      }
      if (this.cfg.promptBtnHandler) {
        this.on("prompt", this.cfg.promptBtnHandler);
      }
    },
    syncUI: function () {
      if (this.cfg.isDraggable) {
        this.boundingBox.draggable({handle: ".window_header"});
      }
      this.boundingBox.css({
        width: this.cfg.width + "px",
        height: this.cfg.height + "px",
        left: (this.cfg.x || ($(document).width() - this.cfg.width) / 2) + "px",
        top: (this.cfg.y || ($(document).height() - this.cfg.height) / 2) + "px"
      });
    },
    destructor: function () {
      this.mask && this.mask.remove();
    },
    alert: function (cfg) {
      $.extend(this.cfg, cfg, {winType: "alert"});
      if (this.boundingBox != null) {
        return;
      }
      this.render();
      return this;
    },
    confirm: function (cfg) {
      $.extend(this.cfg, cfg, {winType: "confirm"});
      if (this.boundingBox != null) {
        return;
      }
      this.render();
      return this;
    },
    prompt: function (cfg) {
      $.extend(this.cfg, cfg, {winType: "prompt"});
      if (this.boundingBox != null) {
        return;
      }
      this.render();
      return this;
    }
  });

  return new Window();
});